<?php

namespace app\api\controller;

use app\api\validate\TokenGet;
use app\common\BaseHelper;

use app\admin\model\Activity as ActivityMdl;
use app\admin\model\Coupon;
use app\admin\model\Membercoupon;
use app\admin\model\Product;
use think\Db;
use think\Exception;

class Activity extends Base
{
    /*
     * 获取活动详情
     * */
    public function getDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->memberId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '会员ID(memberId) 不能为空');
        }
        if (empty($param->id)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'活动Id为空');
        }

        $model = new ActivityMdl();
        $activity = $model->where(['id'=>$param->id])->find();
        //是否有优惠券
        if ($activity['is_coupon'] == 1){
            $couponMdl = new Coupon();
            $couponId = explode(',',$activity['coupon_id']);
            $coupons = [];
            foreach ($couponId as $v){
                $coupon = $couponMdl->where('id',$v)->find();
                if ($coupon) {
                    // 是否已领取
                    $isReceive = Membercoupon::where(['member_id' => $param->memberId, 'coupon_id' => $v])->field('id')->find();
                    if ($isReceive) {
                        $coupon['isReceive'] = 1;
                    } else {
                        $coupon['isReceive'] = 2;
                    }
                    $coupons[] = $coupon;
                }
            }

            $activity['coupon'] = $coupons;
        }
        //是否有商品
        if ($activity['is_product'] == 1){
            $productMdl = new Product();
            $proId = explode(',',$activity['pro_id']);

            foreach ($proId as $v){
                $product[] = $productMdl->where('id',$v)->find();
            }

            $activity['product'] = $product;
        }

        return show(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS,$activity);
    }
}
